| Conditions | 24 |
| Total Lines | 353 |
| Code Lines | 231 |
| Lines | 353 |
| Ratio | 100 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like script.js ➔ createMap often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /* |
||
| 71 | function createMap(mapOpts, poi) { |
||
| 72 | |||
| 73 | // const mapOpts = olMapData[0].mapOpts; |
||
| 74 | // const poi = olMapData[0].poi; |
||
| 75 | |||
| 76 | if (!olEnable) { |
||
| 77 | return; |
||
| 78 | } |
||
| 79 | if (!olTestCSSsupport()) { |
||
| 80 | olEnable = false; |
||
| 81 | return; |
||
| 82 | } |
||
| 83 | |||
| 84 | // find map element location |
||
| 85 | var cleartag = document.getElementById(mapOpts.id + '-clearer'); |
||
| 86 | if (cleartag === null) { |
||
| 87 | return; |
||
| 88 | } |
||
| 89 | // create map element and add to document |
||
| 90 | var fragment = olCreateMaptag(mapOpts.id, mapOpts.width, mapOpts.height); |
||
| 91 | cleartag.parentNode.insertBefore(fragment, cleartag); |
||
| 92 | |||
| 93 | |||
| 94 | const overlayGroup = new ol.layer.Group({title: 'Overlays', fold: 'open', layers: []}); |
||
| 95 | const baseLyrGroup = new ol.layer.Group({'title': 'Base maps', layers: []}); |
||
| 96 | |||
| 97 | const map = new ol.Map({ |
||
| 98 | target: document.getElementById(mapOpts.id), |
||
| 99 | layers: [baseLyrGroup, overlayGroup], |
||
| 100 | view: new ol.View({ |
||
| 101 | center: ol.proj.transform([mapOpts.lon, mapOpts.lat], 'EPSG:4326', 'EPSG:3857'), |
||
| 102 | zoom: mapOpts.zoom, |
||
| 103 | projection: 'EPSG:3857' |
||
| 104 | }) |
||
| 105 | }); |
||
| 106 | |||
| 107 | let /** |
||
| 108 | * Thunderforest API key. |
||
| 109 | * |
||
| 110 | * @type {String} |
||
| 111 | */ |
||
| 112 | tfApiKey = ''; |
||
| 113 | let /** |
||
| 114 | * OSM tiles flag. |
||
| 115 | * |
||
| 116 | * @type {Boolean} |
||
| 117 | */ |
||
| 118 | osmEnable = true; |
||
| 119 | if (osmEnable) { |
||
| 120 | baseLyrGroup.getLayers().push( |
||
| 121 | new ol.layer.Tile({ |
||
| 122 | visible: true, |
||
| 123 | title: 'OSM', |
||
| 124 | type: 'base', |
||
| 125 | source: new ol.source.OSM() |
||
| 126 | })); |
||
| 127 | |||
| 128 | baseLyrGroup.getLayers().push( |
||
| 129 | new ol.layer.Tile({ |
||
| 130 | visible: mapOpts.baselyr === "cycle map", |
||
| 131 | title: 'cycle map', |
||
| 132 | type: 'base', |
||
| 133 | source: new ol.source.OSM({ |
||
| 134 | url: 'https://{a-c}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png?apikey=' + tfApiKey, |
||
| 135 | attributions: 'Data ©ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, ' |
||
| 136 | + 'Tiles ©<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>' |
||
| 137 | + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>' |
||
| 138 | }) |
||
| 139 | })); |
||
| 140 | |||
| 141 | baseLyrGroup.getLayers().push( |
||
| 142 | new ol.layer.Tile({ |
||
| 143 | visible: mapOpts.baselyr === "transport", |
||
| 144 | title: 'transport', |
||
| 145 | type: 'base', |
||
| 146 | source: new ol.source.OSM({ |
||
| 147 | url: 'https://{a-c}.tile.thunderforest.com/transport/{z}/{x}/{y}.png?apikey=' + tfApiKey, |
||
| 148 | attributions: 'Data ©ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, ' |
||
| 149 | + 'Tiles ©<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>' |
||
| 150 | + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>' |
||
| 151 | }) |
||
| 152 | })); |
||
| 153 | |||
| 154 | baseLyrGroup.getLayers().push( |
||
| 155 | new ol.layer.Tile({ |
||
| 156 | visible: mapOpts.baselyr === "landscape", |
||
| 157 | title: 'landscape', |
||
| 158 | type: 'base', |
||
| 159 | source: new ol.source.OSM({ |
||
| 160 | url: 'https://{a-c}.tile.thunderforest.com/landscape/{z}/{x}/{y}.png?apikey=' + tfApiKey, |
||
| 161 | attributions: 'Data ©ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, ' |
||
| 162 | + 'Tiles ©<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>' |
||
| 163 | + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>' |
||
| 164 | }) |
||
| 165 | })); |
||
| 166 | |||
| 167 | baseLyrGroup.getLayers().push( |
||
| 168 | new ol.layer.Tile({ |
||
| 169 | visible: mapOpts.baselyr === "outdoors", |
||
| 170 | title: 'outdoors', |
||
| 171 | type: 'base', |
||
| 172 | source: new ol.source.OSM({ |
||
| 173 | url: 'https://{a-c}.tile.thunderforest.com/outdoors/{z}/{x}/{y}.png?apikey=' + tfApiKey, |
||
| 174 | attributions: 'Data ©ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, ' |
||
| 175 | + 'Tiles ©<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>' |
||
| 176 | + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>' |
||
| 177 | }) |
||
| 178 | })); |
||
| 179 | } |
||
| 180 | |||
| 181 | let /** |
||
| 182 | * Bing tiles flag. |
||
| 183 | * |
||
| 184 | * @type {Boolean} |
||
| 185 | */ |
||
| 186 | bEnable = false; |
||
| 187 | let /** |
||
| 188 | * Bing API key. |
||
| 189 | * |
||
| 190 | * @type {String} |
||
| 191 | */ |
||
| 192 | bApiKey = ''; |
||
| 193 | if (bEnable && bApiKey !== '') { |
||
| 194 | baseLyrGroup.getLayers().push( |
||
| 195 | new ol.layer.Tile({ |
||
| 196 | visible: mapOpts.baselyr === "bing road", |
||
| 197 | title: 'bing road', |
||
| 198 | type: 'base', |
||
| 199 | source: new ol.source.BingMaps({ |
||
| 200 | key: bApiKey, |
||
| 201 | imagerySet: 'Road' |
||
| 202 | }) |
||
| 203 | })); |
||
| 204 | |||
| 205 | baseLyrGroup.getLayers().push( |
||
| 206 | new ol.layer.Tile({ |
||
| 207 | visible: mapOpts.baselyr === "bing sat", |
||
| 208 | title: 'bing sat', |
||
| 209 | type: 'base', |
||
| 210 | source: new ol.source.BingMaps({ |
||
| 211 | key: bApiKey, |
||
| 212 | imagerySet: 'Aerial' |
||
| 213 | }) |
||
| 214 | })); |
||
| 215 | |||
| 216 | baseLyrGroup.getLayers().push( |
||
| 217 | new ol.layer.Tile({ |
||
| 218 | visible: mapOpts.baselyr === "bing hybrid", |
||
| 219 | title: 'bing hybrid', |
||
| 220 | type: 'base', |
||
| 221 | source: new ol.source.BingMaps({ |
||
| 222 | key: bApiKey, |
||
| 223 | imagerySet: 'AerialWithLabels' |
||
| 224 | }) |
||
| 225 | })); |
||
| 226 | } |
||
| 227 | |||
| 228 | let /** |
||
| 229 | * Stamen tiles flag. |
||
| 230 | * |
||
| 231 | * @type {Boolean} |
||
| 232 | */ |
||
| 233 | stamenEnable = false; |
||
| 234 | if (stamenEnable) { |
||
| 235 | baseLyrGroup.getLayers().push( |
||
| 236 | new ol.layer.Tile({ |
||
| 237 | visible: false, |
||
| 238 | type: 'base', |
||
| 239 | title: 'toner', |
||
| 240 | source: new ol.source.Stamen({layer: 'toner'}) |
||
| 241 | }) |
||
| 242 | ); |
||
| 243 | baseLyrGroup.getLayers().push( |
||
| 244 | new ol.layer.Tile({ |
||
| 245 | visible: false, |
||
| 246 | type: 'base', |
||
| 247 | title: 'terrain', |
||
| 248 | source: new ol.source.Stamen({layer: 'terrain'}) |
||
| 249 | }) |
||
| 250 | ); |
||
| 251 | } |
||
| 252 | |||
| 253 | map.addControl(new ol.control.ScaleLine({bar: true, text: true})); |
||
| 254 | map.addControl(new ol.control.MousePosition({ |
||
| 255 | coordinateFormat: ol.coordinate.createStringXY(4), projection: 'EPSG:4326', |
||
| 256 | })); |
||
| 257 | map.addControl(new ol.control.FullScreen()); |
||
| 258 | map.addControl(new ol.control.OverviewMap()); |
||
| 259 | map.addControl(new ol.control.LayerSwitcher()); |
||
| 260 | |||
| 261 | const iconScale = 1.5; |
||
| 262 | const vectorSource = new ol.source.Vector(); |
||
| 263 | poi.forEach((p) => { |
||
| 264 | const f = new ol.Feature({ |
||
| 265 | geometry: new ol.geom.Point(ol.proj.fromLonLat([p.lon, p.lat])), |
||
| 266 | description: p.txt, |
||
| 267 | img: p.img, |
||
| 268 | rowId: p.rowId, |
||
| 269 | lat: p.lat, |
||
| 270 | lon: p.lon, |
||
| 271 | angle: p.angle, |
||
| 272 | alt: p.img.substring(0, p.img.lastIndexOf(".")) |
||
| 273 | }); |
||
| 274 | f.setId(p.rowId); |
||
| 275 | f.setStyle(new ol.style.Style({ |
||
| 276 | text: new ol.style.Text({ |
||
| 277 | text: "" + p.rowId, |
||
| 278 | textAlign: 'left', |
||
| 279 | textBaseline: 'bottom', |
||
| 280 | offsetX: 8, |
||
| 281 | offsetY: -8, |
||
| 282 | scale: iconScale, |
||
| 283 | fill: new ol.style.Fill({color: 'rgb(0,0,0)'}), |
||
| 284 | font: '12px monospace bold', |
||
| 285 | backgroundFill: new ol.style.Fill({color: 'rgba(255,255,255,.4)'}) |
||
| 286 | }), image: new ol.style.Icon({ |
||
| 287 | src: DOKU_BASE + "lib/plugins/openlayersmap/icons/" + p.img, |
||
| 288 | crossOrigin: '', |
||
| 289 | opacity: p.opacity, |
||
| 290 | scale: iconScale, |
||
| 291 | rotation: p.angle * Math.PI / 180, |
||
| 292 | }), |
||
| 293 | })); |
||
| 294 | vectorSource.addFeature(f); |
||
| 295 | }); |
||
| 296 | |||
| 297 | overlayGroup.getLayers().push(new ol.layer.Vector({title: 'POI', visible: true, source: vectorSource})); |
||
| 298 | |||
| 299 | if (mapOpts.kmlfile.length > 0) { |
||
| 300 | overlayGroup.getLayers().push(new ol.layer.Vector({ |
||
| 301 | title: 'KML file', visible: true, |
||
| 302 | source: new ol.source.VectorSource({ |
||
| 303 | url: mapOpts.kmlfile, |
||
| 304 | format: new ol.format.KML(), |
||
| 305 | }), |
||
| 306 | style: {label: "${name}"} |
||
| 307 | })); |
||
| 308 | } |
||
| 309 | |||
| 310 | if (mapOpts.geojsonfile.length > 0) { |
||
| 311 | overlayGroup.getLayers().push(new ol.layer.Vector({ |
||
| 312 | title: 'GeoJSON file', visible: true, |
||
| 313 | source: new ol.source.VectorSource({ |
||
| 314 | url: mapOpts.geojsonfile, |
||
| 315 | format: new ol.format.GeoJSON(), |
||
| 316 | }), |
||
| 317 | style: { |
||
| 318 | /* TODO */ |
||
| 319 | strokeColor: "#FF00FF", |
||
| 320 | strokeWidth: 3, |
||
| 321 | strokeOpacity: 0.7, |
||
| 322 | pointRadius: 4, |
||
| 323 | fillColor: "#FF99FF", |
||
| 324 | fillOpacity: 0.7 |
||
| 325 | } |
||
| 326 | })); |
||
| 327 | } |
||
| 328 | |||
| 329 | if (mapOpts.gpxfile.length > 0) { |
||
| 330 | overlayGroup.getLayers().push(new ol.layer.Vector({ |
||
| 331 | title: 'GPS track', visible: true, |
||
| 332 | source: new ol.source.VectorSource({ |
||
| 333 | url: mapOpts.gpxfile, |
||
| 334 | format: new ol.format.GPX(), |
||
| 335 | }), |
||
| 336 | style: { |
||
| 337 | /* TODO */ |
||
| 338 | strokeColor: "#0000FF", |
||
| 339 | strokeWidth: 3, |
||
| 340 | strokeOpacity: 0.7, |
||
| 341 | pointRadius: 4, |
||
| 342 | fillColor: "#0099FF", |
||
| 343 | fillOpacity: 0.7 |
||
| 344 | } |
||
| 345 | })); |
||
| 346 | } |
||
| 347 | |||
| 348 | const container = document.getElementById('popup'); |
||
| 349 | const content = document.getElementById('popup-content'); |
||
| 350 | const closer = document.getElementById('popup-closer'); |
||
| 351 | |||
| 352 | const overlay = new ol.Overlay({ |
||
| 353 | element: container, autoPan: true, autoPanAnimation: { |
||
| 354 | duration: 250, |
||
| 355 | }, //stopEvent: false, |
||
| 356 | }); |
||
| 357 | map.addOverlay(overlay); |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Add a click handler to hide the popup. |
||
| 361 | * @return {boolean} Don't follow the href. |
||
| 362 | */ |
||
| 363 | closer.onclick = function () { |
||
| 364 | overlay.setPosition(undefined); |
||
| 365 | closer.blur(); |
||
| 366 | return false; |
||
| 367 | }; |
||
| 368 | |||
| 369 | // display popup on click |
||
| 370 | map.on('singleclick', function (evt) { |
||
| 371 | const selFeature = map.forEachFeatureAtPixel(evt.pixel, function (feature) { |
||
| 372 | return feature; |
||
| 373 | }); |
||
| 374 | if (selFeature) { |
||
| 375 | overlay.setPosition(evt.coordinate); |
||
| 376 | |||
| 377 | let pContent = '<div class="spacer"> </div>'; |
||
| 378 | let locDesc = ''; |
||
| 379 | |||
| 380 | if (selFeature.get('rowId') !== undefined) { |
||
| 381 | pContent += '<span class="rowId">' + selFeature.get('rowId') + ': </span>'; |
||
| 382 | } |
||
| 383 | if (selFeature.get('name') !== undefined) { |
||
| 384 | pContent += '<span class="txt">' + selFeature.get('name') + '</span>'; |
||
| 385 | locDesc = selFeature.get('name'); |
||
| 386 | // TODO strip <p> tag from locDesc |
||
| 387 | // locDesc = selFeature.get('name').split(/\s+/).slice(0,2).join('+'); |
||
| 388 | } |
||
| 389 | if (selFeature.get('ele') !== undefined) { |
||
| 390 | pContent += '<div class="ele">elevation: ' + selFeature.get('ele') + '</div>'; |
||
| 391 | } |
||
| 392 | if (selFeature.get('type') !== undefined) { |
||
| 393 | pContent += '<div>' + selFeature.get('type') + '</div>'; |
||
| 394 | } |
||
| 395 | if (selFeature.get('time') !== undefined) { |
||
| 396 | pContent += '<div class="time">time: ' + selFeature.get('time') + '</div>'; |
||
| 397 | } |
||
| 398 | if (selFeature.get('description') !== undefined) { |
||
| 399 | pContent += '<div class="desc">' + selFeature.get('description') + '</div>'; |
||
| 400 | } |
||
| 401 | if (selFeature.get('img') !== undefined) { |
||
| 402 | pContent += '<div class="coord" title="lat;lon">' + |
||
| 403 | '<img src="' + DOKU_BASE + 'lib/plugins/openlayersmap/icons/' + selFeature.get('img') + |
||
| 404 | '" width="16" height="16" ' + 'style="transform:rotate(' + selFeature.get('angle') + 'deg)" /> ' + |
||
| 405 | '<a href="geo:' + selFeature.get('lat') + ',' + selFeature.get('lon') + '?q=' + selFeature.get('lat') + |
||
| 406 | ',' + selFeature.get('lon') + '(' + selFeature.get('alt') + ')" title="Open in navigation app">' + |
||
| 407 | ol.coordinate.format([selFeature.get('lon'), selFeature.get('lat')], '{x}º; {y}º', 4) + '</a></div>'; |
||
| 408 | } |
||
| 409 | content.innerHTML = pContent; |
||
| 410 | } else { |
||
| 411 | // do nothing?... |
||
| 412 | } |
||
| 413 | }); |
||
| 414 | |||
| 415 | // change mouse cursor when over marker |
||
| 416 | map.on('pointermove', function (e) { |
||
| 417 | const pixel = map.getEventPixel(e.originalEvent); |
||
| 418 | const hit = map.hasFeatureAtPixel(pixel); |
||
| 419 | map.getTarget().style.cursor = hit ? 'pointer' : ''; |
||
| 420 | }); |
||
| 421 | |||
| 422 | return map; |
||
| 423 | } |
||
| 424 | |||
| 554 |